package com.wxzz.elearing.course.api.admin;


import com.alibaba.fastjson.JSONObject;
import com.wxzz.elearing.course.amqp.producer.CourseProducer;
import com.wxzz.elearing.course.dto.CurriculumCategoryDto;
import com.wxzz.elearing.course.entity.CurriculumCategory;
import com.wxzz.elearing.course.service.ICurriculumCategoryService;
import com.wxzz.elearning.common.exception.ServiceException;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程分类 前端控制器
 * </p>
 *
 * @author
 * @since 2021-03-10
 */
@RestController
@RequestMapping("/curriculum-category")
@Api(tags = "课程分类接口(后台)")
public class CurriculumCategoryController {

    @Autowired
    private ICurriculumCategoryService curriculumCategoryService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private CourseProducer courseProducer;

    @RequestMapping(value = "ackMQSender", method = RequestMethod.POST)
    public void ackMQSender() {
        courseProducer.ackMQSender("TESTST");
    }

    @ApiOperation(value = "课程分类列表 根据当前账号自动筛选(后台)", notes = "课程分类列表 根据当前账号自动筛选(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "listCurriculumCategory", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<CurriculumCategory>>> listCurriculumCategory(@RequestHeader String token, @RequestBody CurriculumCategoryDto curriculumCategoryDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return curriculumCategoryService.listCurriculumCategory(jsonObject.getLong("enterpriseId"), curriculumCategoryDto);
    }

    @ApiOperation(value = "平台课程分类(后台)", notes = "平台课程分类(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "listCurriculumCategory4platform", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<CurriculumCategory>>> listCurriculumCategory4platform(@RequestBody CurriculumCategoryDto curriculumCategoryDto) {
        return curriculumCategoryService.listCurriculumCategory(0L, curriculumCategoryDto);
    }

//    @ApiOperation(value = "列出课程分类搜索(后台)", notes = "列出课程分类搜索(后台)", httpMethod = "POST",tags = "后台")
//    @RequestMapping(value = "listCurriculumCategory", method = RequestMethod.POST)
//    public ReturnVO<List<CurriculumCategory>> listCurriculumCategory(@RequestHeader String token) {
//        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
//        return curriculumCategoryService.listCurriculumCategory(jsonObject.getLong("enterpriseId"));
//    }

    @ApiOperation(value = "新增分类(后台)", notes = "新增分类(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "saveCurriculumCategory", method = RequestMethod.POST)
    public ReturnVO saveCurriculumCategory(@RequestHeader String token, @RequestBody CurriculumCategory curriculumCategory) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        curriculumCategory.setEnterpriseId(jsonObject.getLong("enterpriseId"));
        return curriculumCategoryService.saveCurriculumCategory(curriculumCategory);
    }

    @ApiOperation(value = "修改分类(后台)", notes = "修改分类(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "updateCurriculumCategory", method = RequestMethod.POST)
    public ReturnVO updateCurriculumCategory(@RequestBody CurriculumCategory curriculumCategory) {
        return curriculumCategoryService.updateCurriculumCategory(curriculumCategory);
    }


    @ApiOperation(value = "删除课程分类(后台)", notes = "删除课程分类(后台)", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "deleteCurriculumCategory/{curriculumCategoryId}", method = RequestMethod.POST)
    public ReturnVO deleteCurriculumCategory(@PathVariable Long curriculumCategoryId) {
        return curriculumCategoryService.deleteCurriculumCategory(curriculumCategoryId);
    }
}
